package com.module.oa.util;


import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.api.model.LoginUser;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.Date;

/**
 * 工具类
 */
public class Util {

    /**
     * 根据时间戳生成主键id
     * @return
     */
    public static String get_radom_id(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSSSSSS")).toString();
    }

    /**
     * String转date类型
     * @param date_str
     * @return
     */
    public static Date fomat_date(String date_str){
        Date date = null;
        //实现将字符串转成⽇期类型
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = dateFormat.parse(date_str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 根据业务类型随机生成业务编号
     * @param businessType
     * @return
     */
    public static String get_business_no(String businessType){
        return businessType+"_"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")).toString();
    }

    /**
     * 将汉字转换为拼音首字母
     * @param str
     * @return
     */
    public static String ToPinyin(String str){
        String firstLetter = "";
        int length = str.length();
        for (int i = 0; i < length; i++) {
            char ch = str.charAt(i);
            if (ch >= 0x4e00 && ch <= 0x9fa5) { // 判断是否为汉字
                // 获取汉字的拼音
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(ch);
                if (pinyinArray != null) {
                    // 获取拼音首字母
                    String pinyin = pinyinArray[0];
                    if (pinyin != null) {
                        firstLetter += pinyin.charAt(0);
                    }
                }
            } else {
                firstLetter += ch;
            }
        }
        return firstLetter.toUpperCase().substring(0,2);
    }

    /**
     * 根据角色查人员
     * @return
     */
    public static String selectUserByRoleId(Set<String> role) throws SQLException, ClassNotFoundException {
        String roles = "";
        String result = "";
        // 如果一个人存在多个角色
        if(role!=null&&!role.equals("")){
            if(role.size()>1){
                // 如果有多个角色
                roles = " b.role_key in(";
                Iterator it = role.iterator();
                while (it.hasNext()) {
                    roles += "'"+it.next()+"',";
                }
                roles = roles.substring(0,roles.length()-1)+")";
            }else{
                roles = "b.role_key='"+role.iterator().next()+"'";
            }
        }
        // 动态生成sql
        String sql = "select c.user_id,c.nick_name from sys_user c,(select a.user_id from sys_user_role a where a.role_id in (select b.role_id from sys_role b where "+roles+" and b.del_flag='0' and b.status='0')) d where c.user_id=d.user_id and c.del_flag='0' and c.status='0'";
        Connection conn = connMysql();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()){
            result += rs.getString("user_id")+","+rs.getString("nick_name")+"-";
        }
        rs.close();
        stmt.close();
        conn.close();
        return result.substring(0,result.length()-1);
    }

    /**
     * 公用mysql连接方法
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static Connection connMysql() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ruoyi_system", "root", "root123");
        return conn;
    }

    public static List<SysRole> getAllRole(String sql) throws SQLException, ClassNotFoundException {
        List<SysRole> list = new ArrayList<SysRole>();
        // 连接mysql
        Connection conn = connMysql();
        Statement stmt =conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()){
            //将数据实体循环添加到list中
            SysRole sysRole = new SysRole();
            sysRole.setRoleId(rs.getLong("role_id"));
            sysRole.setRoleKey(rs.getString("role_key"));
            sysRole.setRoleName(rs.getString("role_name"));
            list.add(sysRole);
        }
        rs.close();
        stmt.close();
        conn.close();
        return list;
    }

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Set<String> role = new HashSet<>();
        role.add("mz-ptyg");
        System.out.println(selectUserByRoleId(role));
    }
}
